package com.jiazhong.mybatis.m1;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.mybatis.m1.bean.Emp;
import com.jiazhong.mybatis.m1.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {

    private static void a() {
        // 1. 加载config.xml
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 产生会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4. 产生mapper的对象
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 5. 调用方法
        List<Emp> list = empMapper.findAll();
        for (Emp emp : list) {
            System.out.println(emp);
        }
        // 6. 关闭
        sqlSession.close();
    }

    private static void b() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.findById(7909);
        System.out.println(emp);
        sqlSession.close();
    }

    private static void c() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.findByName("'%张%'");
        for (Emp emp : list) {
            System.out.println(emp);
        }
        sqlSession.close();
    }

    private static void d1() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.findByEnameAndJob1("%张%", "程序员");
        for (Emp emp : list) {
            System.out.println(emp);
        }
        sqlSession.close();
    }

    private static void d2() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 将2个参数封装层一个参数
        Emp emp = new Emp();
        emp.setEname("%张%");
        emp.setJob("程序员");
        List<Emp> list = empMapper.findByEnameAndJob2(emp);
        for (Emp e : list) {
            System.out.println(e);
        }
        sqlSession.close();
    }

    private static void d3() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 将2个参数封装层一个参数
        Map map = new HashMap();
        map.put("ename", "%张%");
        map.put("job", "程序员");
        List<Emp> list = empMapper.findByEnameAndJob3(map);
        for (Emp e : list) {
            System.out.println(e);
        }
        sqlSession.close();
    }

    private static void d4() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.findByEnameAndJob4("%张%", "程序员");
        for (Emp emp : list) {
            System.out.println(emp);
        }
        sqlSession.close();
    }

    private static void e() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEname("程佳彪");
        emp.setJob("程序员");
        emp.setMgr(7980);
        emp.setSal(9876.0);
        emp.setComm(null);
        emp.setDeptNo(40);
        int row = empMapper.save(emp);
        System.out.println(row);
        // 提交事务
        sqlSession.commit(); // sqlSession.rollback();
        sqlSession.close();
    }

    private static void f() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setEmpNo(8047);
        emp.setEname("王田凤岐");
        emp.setJob("歌手");
        emp.setMgr(8050);
        emp.setSal(9176.0);
        emp.setComm(9879.90);
        emp.setDeptNo(20);
        int row = empMapper.update(emp);
        System.out.println(row);
        // 提交事务
        sqlSession.commit(); // sqlSession.rollback();
        sqlSession.close();
    }
    private static void g() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

        int row = empMapper.remove(7908);
        System.out.println(row);
        // 提交事务
        sqlSession.commit(); // sqlSession.rollback();
        sqlSession.close();
    }


    private static void h() {
        // 1. 加载config.xml
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        // 2. 产生会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 产生会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4. 产生mapper的对象
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 5. 调用方法
        int page = 2; // 页码
        int size = 10; // 每页显示条数
        PageHelper.startPage(page, size);
        List<Emp> list = empMapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo);
        // 6. 关闭
        sqlSession.close();
    }
    public static void main(String[] args) {
        h();
    }
}